#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import shutil
import datetime

timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%c')




# rootDir = '/Users/daniellu/Desktop/ez/src/main/java/com/amita/ez'
rootDir = '.'
tpFile = rootDir + "/web/demo.java.txt"


def getFname(fname):
    basename, ext = os.path.splitext(fname)
    fpath = basename.lower()
    return (fpath, basename, ext)


def readTp():
    with open(tpFile, 'r') as tp:
        return tp.read()


def genCode(tp, item):
    (fpath, basename, ext) = item
    sname = basename[0].lower() + basename[1:len(basename)]
    txt = tp.replace('#Entity#', basename).replace(
        '#fpath#', fpath).replace('#sname#', sname).replace("#timestamp#", timestamp)
    toJava = '{}/web/{}Ctrl{}'.format(rootDir, basename, ext)
    print(toJava)
    with open(toJava, 'w') as f:
        f.write(txt)






def main():

    # files = os.listdir(rootDir + '/entity')
    # # print(files)
    files =  ['SysRole.java']
    webs = [getFname(fname)
            for fname in files if fname.endswith(".java")]

    tp = readTp()
    # print(tp)

    [genCode(tp, item) for item in webs]


if __name__ == '__main__':

    main()
    # row = ('ACME', 50, 91.5)
    # print(*row, sep="**")
    print('----success')
